package com.navinfo.collect.library.data

import android.util.Log
import com.navinfo.collect.library.data.entity.CustomLayerItem
import com.navinfo.collect.library.data.entity.DataLayerItemType
import com.navinfo.collect.library.data.entity.Element
import com.navinfo.collect.library.data.entity.LayerManager
import org.json.JSONArray
import org.json.JSONObject

/**
 * 和flutter层进行数据传递时的数据格式转换
 */
class DataConversion {
    companion object {
        /**
         * 将数据转成map形式
         */
        fun toElementMapList(list: List<Element>): List<Map<*, *>> {
            val newList = mutableListOf<Map<*, *>>()
            for (element in list) {
                newList.add(
                    mapOf(
                        "layerId" to element.layerId,
                        "uuid" to element.id,
                        "geometry" to element.geometry,
                        "displayText" to element.displayText,
                        "tOperateDate" to element.operationTime,
                        "tLifecycle" to element.tLifecycle,
                        "tStatus" to element.tStatus,
                        "values" to element.values
                    ),
                )
            }
            return newList
        }

        /**
         * 将数据图层自定义子表所有配置转成JSON形式
         * （现在用在向数据库插入数据图层表时，主要用于原生层）
         */
        fun layerItemsToJson(list: List<CustomLayerItem>): String {
            val jsonArray = JSONArray()
            for (item in list) {
                val jsonObject = JSONObject()
                jsonObject.put("key", item.key)
                jsonObject.put("title", item.title)
                jsonObject.put("type", item.type.ordinal)
                jsonObject.put("nullable", item.nullable)
                jsonObject.put("primaryKey", item.primaryKey)
                jsonObject.put("value", item.value)
                jsonObject.put("selectOptions", item.selectOptions)
                jsonObject.put("isMainName", item.isMainName)
                jsonObject.put("describe", item.describe)
                jsonArray.put(jsonObject)
            }
            return jsonArray.toString()
        }

        /**
         * json转数据图层自定义子表的所有字段对象列表
         * （现在用于从数据库中提取数据图层自定义表所有字段信息时，主要用于原生层）
         */
        fun jsonToLayerItemsList(json: String): List<CustomLayerItem> {
            val list = mutableListOf<CustomLayerItem>()
            try {
                val jsonArray = JSONArray(json)
                for (i in 0 until jsonArray.length()) {
                    val itemObject = jsonArray.getJSONObject(i)
                    val itemMap = jsonToCustomLayerItem(itemObject)
                    list.add(itemMap);
                }
            } catch (e: Throwable) {
                Log.e("jingo", e.message)
                Log.e("jingo", e.stackTraceToString())
            }
            return list
        }


        /**
         * json转数据图层自定义子表的所有字段map列表
         * （现在用于从数据库提取数数据后转成flutter要使用的 map形式，主要用于flutter与原生交互）
         */
        fun jsonToLayerItemsMap(json: String): List<Map<String, *>> {
            val list = mutableListOf<Map<String, *>>()
            try {
                val jsonArray = JSONArray(json)
                for (i in 0 until jsonArray.length()) {
                    val itemObject = jsonArray.getJSONObject(i)
                    val itemMap = customLayerItemToMap(jsonToCustomLayerItem(itemObject))
                    list.add(itemMap);
                }
            } catch (e: Throwable) {
                Log.e("jingo", e.message)
                Log.e("jingo", e.stackTraceToString())
            }
            return list
        }

        /**
         * 数据图层子表字段配置转Map
         * （主要flutter与原生交互时使用）
         */
        private fun customLayerItemToMap(itemObject: CustomLayerItem): Map<String, *> {
            return mapOf(
                "key" to itemObject.key,
                "title" to itemObject.title,
                "type" to itemObject.type.ordinal,
                "nullable" to itemObject.nullable,
                "primaryKey" to itemObject.primaryKey,
                "value" to itemObject.value,
                "selectOptions" to itemObject.selectOptions,
                "isMainName" to itemObject.isMainName,
                "describe" to itemObject.describe
            )
        }

        /**
         * 数据图层子表字段配置转Map
         * （主要原生层使用）
         */
        fun customLayerItemsToMapList(list: List<CustomLayerItem>?): List<Map<*, *>> {
            var newList = mutableListOf<Map<*, *>>()
            if (list == null) {
                return newList
            }

            for (item in list) {
                newList.add(customLayerItemToMap(item))
            }
            return newList
        }

        /**
         * 数据图层子表字段配置 json形式的数据 转成对象形式
         * （原生层主要用json，所以主要原生层使用）
         */
        private fun jsonToCustomLayerItem(itemObject: JSONObject): CustomLayerItem {
            return CustomLayerItem(
                key = itemObject.optString("key"),
                title = itemObject.optString("title"),
                type = DataLayerItemType.values()[itemObject.optInt("type")],
                nullable = itemObject.optBoolean("nullable"),
                primaryKey = itemObject.optBoolean("primaryKey"),
                value = itemObject.opt("value"),
                selectOptions = itemObject.optString("selectOptions"),
                isMainName = itemObject.optBoolean("isMainName"),
                describe = itemObject.optString("describe"),
            )
        }

        /**
         * 数据图层子表字段配置转Map
         * （flutter层喜欢用map，所以主要用于flutter和原生交互使用）
         */
        private fun jsonToCustomLayerItem(itemMap: Map<*, *>): CustomLayerItem {
            return CustomLayerItem(
                key = itemMap["key"] as String,
                title = itemMap["title"] as String,
                type = DataLayerItemType.values()[itemMap["type"] as Int],
                nullable = itemMap["nullable"] as Boolean,
                primaryKey = itemMap["primaryKey"] as Boolean,
                value = itemMap["value"] as Any,
                selectOptions = itemMap["selectOptions"] as String,
                isMainName = itemMap["isMainName"] as Boolean,
                describe = itemMap["describe"] as String
            )
        }

        /**
         * 数据图层 map形式的字段数据集合转成对象集合
         * （主要用于flutter层与原生层数据交互时）
         */
        fun listMapToCustomLayerItemsList(itemList: List<Map<*, *>>): List<CustomLayerItem> {
            val list = mutableListOf<CustomLayerItem>()
            for (itemMap in itemList) {
                list.add(jsonToCustomLayerItem(itemMap));
            }
            return list
        }

//        /**
//         * layerManager转化成map
//         */
//        fun layerManagerToMap(
//            layerManager: LayerManager,
//            itemsMap: List<Map<String, *>>
//        ): Map<*, *> {
//            return mapOf("layerManager" to layerManager.toMap(), "itemsList" to itemsMap);
//        }

    }
}